package com.bonana.core.services.impl;

import com.bonana.core.dao.index.BannerMapper;
import com.bonana.core.entity.mybatis.Banner;
import com.bonana.core.entity.mybatis.BannerExample;
import com.bonana.core.services.BannerService;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/4.
 */
@Service
public class BannerServiceImpl implements BannerService
{
    private Logger logger = LogManager.getLogger(BannerServiceImpl.class);
    @Autowired
    BannerMapper bannerMapper;

    @Override
    public List<Banner> getTopBanner() {
        logger.info("getTopBanner calls");
        BannerExample be = new BannerExample();
        Date now = new Date();
        be.createCriteria().andBannerLocEqualTo(BannerService.TopBanner).andBannerStarttimeLessThanOrEqualTo(now).andBannerEndtimeGreaterThan(now);
        return bannerMapper.selectByExample(be);
    }

    @Override
    public List<Banner> getAdBanner() {
        BannerExample be = new BannerExample();
        Date now = new Date();
        be.createCriteria().andBannerLocEqualTo(BannerService.AdBanner).andBannerStarttimeLessThanOrEqualTo(now).andBannerEndtimeGreaterThan(now);
        return bannerMapper.selectByExample(be);
    }

    @Override
    public int addBanner(Banner bner) {
        return bannerMapper.insert(bner);
    }

    @Override
    public int delBanner(int bannerId) {
        return bannerMapper.deleteByPrimaryKey(bannerId);
    }

    @Override
    public Banner getBanerById(int bannerId) {
        return bannerMapper.selectByPrimaryKey(bannerId);
    }

    @Override
    public int updateBanner(Banner bner) {
        return bannerMapper.updateByPrimaryKey(bner);
    }

    @Override
    public List<Banner> getBanners(int pgNum, int pgSize, int type, String condition) {
        BannerExample example = new BannerExample();
        if(type > 0) {
            if(condition != null && !"".equals(condition)){
                example.createCriteria().andBannerLocEqualTo(type).andBannerTitleLike("%"+condition+"%");
            } else {
                example.createCriteria().andBannerLocEqualTo(type);
            }
        } else {
            if(condition != null && !"".equals(condition)){
                example.createCriteria().andBannerTitleLike("%"+condition+"%");
            }
        }
        PageHelper.startPage(pgNum, pgSize);
        example.setOrderByClause("banner_endtime asc");
        List<Banner> list = bannerMapper.selectByExample(example);
        return list;
    }
}
